﻿var tankGame = new Kiwi.Game();
var tankState = new Kiwi.State('tankState');
tankGame.states.addState(tankState);
tankGame.states.switchState('tankState');

//first preload
tankState.preload = function () {
    debugger;
    Kiwi.State.prototype.preload.call(this);
    this.addSpriteSheet('M1A1TankBase', '/Content/Graphics/M1A1Tank/M1A1TankBase.png', 200, 150);
    this.addSpriteSheet('M1A1TankCannon', '/Content/Graphics/M1A1Tank/M1A1TankCannon2.png', 200, 150);
}

//second creating
tankState.create = function () {
    debugger;
    Kiwi.State.prototype.create.call(this);
    this.tank = new Kiwi.GameObjects.Sprite(this, this.textures.M1A1TankBase, 0, 0);
    this.tankCannon = new Kiwi.GameObjects.Sprite(this, this.textures.M1A1TankCannon, 0, 0);

    this.leftKey = this.game.input.keyboard.addKey(Kiwi.Input.Keycodes.A);
    this.rightKey = this.game.input.keyboard.addKey(Kiwi.Input.Keycodes.D);
    this.downKey = this.game.input.keyboard.addKey(Kiwi.Input.Keycodes.S);
    this.upKey = this.game.input.keyboard.addKey(Kiwi.Input.Keycodes.W);
    this.backspace = this.game.input.keyboard.addKey(Kiwi.Input.Keycodes.Z);

    this.tank.animation.add('idleup', [0], 0.1, false);
    this.tankCannon.animation.add('idleup', [0], 0.1, false);
    this.tankCannon.animation.add('shootup', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 0.01, true);

    this.facing = 'up';
    this.tank.animation.play('idleup');
    this.tankCannon.animation.play('idleup');

    this.addChild(this.tank);
    this.addChild(this.tankCannon);
}

tankState.update = function () {
    Kiwi.State.prototype.update.call(this);
    if (this.backspace.isDown) {
        if (this.tankCannon.animation.currentAnimation.name != ('shoot' + this.facing))
            this.tankCannon.animation.play('shoot' + this.facing);
    }
    else {
        this.tank.animation.play('idle' + this.facing);
        this.tankCannon.animation.play('idle' + this.facing);
    }
}